package com.defense.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

/**
* @类名  ConnUtil.java
* @作者  jinbanglong
* @版本  V1.0
* @日期  2019年6月29日-下午6:32:55
* @描述  数据库连接
*
*/
public class ConnUtil {
	public static String url="";
	public static String user="";
	public static String password="";
	static {
		Properties p = new Properties();
		InputStream rs = ConnUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
		try {
			p.load(rs);
			url=p.getProperty("jdbc.url");
			user=p.getProperty("jdbc.user");
			password=p.getProperty("jdbc.pwd");
			
			Class.forName(p.getProperty("jdbc.Driver"));
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static Connection getConnection(){
		Connection connection;
		try {
			connection = DriverManager.getConnection(url, user, password);
			return connection;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	public static void closeAll(Connection conn,PreparedStatement ps) {
		try {
			if(conn!=null) {
				conn.close();
			}
			if(ps!=null) {
				ps.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public static void closeAll(Connection conn,PreparedStatement ps,ResultSet eq ) {
		try {
			if(conn!=null) {
				conn.close();
			}
			if(ps!=null) {
				ps.close();
			}
			if(eq!=null) {
				eq.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
}
